<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 这是图片 -->
            <div class="img_box">
                <img src="../assets/img/呵呵.jpg" alt="logo">
            </div>
            <!-- 这是表单元素 -->
            <el-form label-width="0px" ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form">
                <el-form-item prop="aphone">
                    <el-input v-model="loginForm.aphone" prefix-icon="el-icon-user-solid"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" type="password" prefix-icon="el-icon-s-cooperation"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
export default {
    data(){
        return{
            //这是表单绑定的数据对象
            loginForm:{
                aphone:'18611111111',
                password:'111111'
            },
            //这是表单的验证规则对象
            loginFormRules:{
                //验证手机号
                aphone:[
                    {required: true, message: '请输入手机号', trigger: 'blur'},
                    {min: 11,max:11, message: '手机号格式有误', trigger: 'blur'}
                ],
                //验证密码
                password:[
                    {required: true, message: '请输入密码', trigger: 'blur'},
                    {min: 6, message: '密码最少6位', trigger: 'blur'}
                ]
            }
            
        }
    },
    methods:{
        //点击重置
        resetLoginForm(){
            this.$refs.loginFormRef.resetFields();
        },
        //完整登录
        login(){
            this.$refs.loginFormRef.validate(async valid => {
                if(!valid){
                    //没有符合基本的校验规则，不与提交
                    return;
                }else{
                    //符合规则，发送到后台
                    const {data:result} = await this.$http.post("/admin/loginByPwd",this.loginForm);
                   
                    if(result.code == 200){
                        //登录信息，验证成功，先给个提示
                        this.$message.success('登录成功，欢迎你：'+result.data.adminName)
                        //保存用户的token，到客户端的sessionStorage
                        //管理页面，除了登录页之外，其他页面都需要登录后才能看
                        //token应该在网站打开期间生效
                        window.sessionStorage.setItem("token",result.msg)
                        //跳转到管理员首页
                        this.$router.push("/home");
                    }
                     if(result.code == 403){
                        this.$message.error('密码错误')
                    }
                    if(result.code == 406){
                        this.$message.error('手机号错误')
                    }
                }
            });
        }
    }
    
}
</script>
<style lang="less" scoped>
.login_container{
    background-color: darkgray;
    height: 100%;
}
.login_box{
    width: 50%;
    height: 40%;
    background-color: whitesmoke;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
}
.img_box{
    width:200px;
    height: 200px;
    border: 1px solid #eeeeee;
    border-radius: 50%;
    padding: 11px;
    box-shadow: 0 0 11px grey;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color:whitesmoke;
    img{
        width:100%;
        height: 100%;
        border-radius: 50%;
        background-color:grey;
    }
}
.btns{
    display: flex;
    justify-content: flex-end;
}
.login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 24px;
    box-sizing: border-box;
}
</style>